package fr.upmc.alasca.centreCalcul;

import java.util.ArrayList;
import java.util.List;

import fr.upmc.alasca.centreCalcul.externe.generateurDemandes.GenerateurDemandes;
import fr.upmc.alasca.centreCalcul.interne.controleurAdmission.ControleurAdmission;
import fr.upmc.alasca.centreCalcul.interne.machinePhysique.MachinePhysique;
import fr.upmc.components.ComponentI;
import fr.upmc.components.cvm.AbstractDistributedCVM;

/**
 * @author Antoine Lavaire, Sabri Labassi, Thibaut Fleury, Ryme Fantazi
 */
public	class	DistributedCVM
extends			AbstractDistributedCVM
{
	protected static final String	URI_CONTROLEUR_ADMISSION 	= "Controleur_Admission";
	protected static final String	URI_MACHINE_PHYSIQUE_1		= "Machine_Physique_1";
	protected static final String	URI_MACHINE_PHYSIQUE_2		= "Machine_Physique_2";
	protected static final String	URI_MACHINE_PHYSIQUE_3		= "Machine_Physique_3";
	protected static final String	URI_MACHINE_PHYSIQUE_4		= "Machine_Physique_4";
	protected static final String	URI_GENERATEUR_DEMANDES		= "Generateur_Demandes";
	
	protected ComponentI cps;




	public		DistributedCVM(String[] args)
	throws Exception
	{
		super(args);
	}
	
	
	/*@Override
	public void			initialise()
	throws Exception
	{
		super.initialise();
	}*/
	
	
	@Override
	public void			instantiateAndPublish()
	throws Exception
	{
		cps = null;
		
		if (thisJVMURI.equals(URI_MACHINE_PHYSIQUE_1) || 
				thisJVMURI.equals(URI_MACHINE_PHYSIQUE_2) ||
				thisJVMURI.equals(URI_MACHINE_PHYSIQUE_3) ||
				thisJVMURI.equals(URI_MACHINE_PHYSIQUE_4)) {
			cps = new MachinePhysique(thisJVMURI + "-dcc", true, 3, 1.5f, 3.0f);
		}
		else if (thisJVMURI.equals(URI_CONTROLEUR_ADMISSION)) {
			List<String> listeMPUri = new ArrayList<String>();
			listeMPUri.add(URI_MACHINE_PHYSIQUE_1+ "-dcc");
			listeMPUri.add(URI_MACHINE_PHYSIQUE_2+ "-dcc");
			listeMPUri.add(URI_MACHINE_PHYSIQUE_3+ "-dcc");
			listeMPUri.add(URI_MACHINE_PHYSIQUE_4+ "-dcc");
			
			cps = new ControleurAdmission(
						URI_CONTROLEUR_ADMISSION + "-dcc",
						true,
						listeMPUri, 1.5f, 3.0f);
		}
		else if (thisJVMURI.equals(URI_GENERATEUR_DEMANDES)) {
			cps = new GenerateurDemandes(
					(Boolean)true,
					(Integer)2,			// Nombre applications
					(Long)2000L,		// Moyenne requêtes
					(Long)3000000000L,	// Moyenne instructions
					(Long)500000000L,	// Ecart type
					URI_CONTROLEUR_ADMISSION + "-dcc");
		}
		
		if (cps == null) {
			System.out.println("Unknow JVM URI : " + thisJVMURI);
		}
		else {
			this.deployedComponents.add(cps);
		}
		
		super.instantiateAndPublish();
	}
	
	
	@Override
	public void			interconnect()
	throws Exception
	{
		assert this.instantiationAndPublicationDone;

		super.interconnect();
	}
	
	
	@Override
	public void			shutdown()
	throws Exception
	{
		super.shutdown();
	}
	
	
	@Override
	public void deploy() throws Exception{
		super.deploy();
		
		if (thisJVMURI.equals(URI_CONTROLEUR_ADMISSION)) {
			((ControleurAdmission)cps).deploy();
		}
		
		if (thisJVMURI.equals(URI_GENERATEUR_DEMANDES)) {
			((GenerateurDemandes)cps).deploy();
		}
	}
	
	
	public static void	main(String[] args)
	{
		try {
			DistributedCVM dCVM = new DistributedCVM(args);
			
			System.out.println("Deploying...");
			dCVM.deploy();
			
			System.out.println("Starting...");
			dCVM.start();
			
			// Simulation durant 15 minutes
			//Thread.sleep(20000);
			Thread.sleep(15 * 60 * 1000);
			
			System.out.println("Ending...");
			dCVM.shutdown();
			
			System.exit(0);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
